package com.weasel.http.builder;

import com.weasel.http.helper.HttpUrlHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * Created by Dylan on 2015/10/8.
 */
public class HttpGetMethodBuilder extends AbstractHttpMethodBuilder<HttpGetMethodBuilder> {

    private HttpGet get = new HttpGet();

    public HttpGetMethodBuilder withParams(Map<String,String> params){

        String uri  = StringUtils.substringBefore(getMethod().getURI().toString(),"?");
        HttpUrlBuilder urlBuilder = HttpUrlHelper.withURL(uri);
        params.forEach((k,v)->{
            urlBuilder.addParam(k,v);
        });
        try {
            getMethod().setURI(new URI(urlBuilder.build()));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return this;

    }

    @Override
    public HttpGet getMethod() {
        return get;
    }
}
